package leetcode.code1588;

import java.util.LinkedList;
import java.util.Queue;

public class Solution extends Solution1588 {

	@Override
	public int sumOddLengthSubarrays(int[] arr) {
		int len = arr.length;// 数组长度
		int sum = 0;// 累加和
		Queue<Integer> times = new LinkedList<>();
		for (int i = 0; i < len; i++) {
			int num = len - i; // 包含i后边有几个数
			int maxj = 0;
			if (num % 2 == 1) {
				maxj = num;
			} else {
				maxj = num - 1;
			}
			int temp = times.size();
			while (temp > 0) {
				temp--;
				int head = times.poll() - 1;
				if (head > 0) {
					times.add(head);
				}
			}
			while (maxj > 1) {// 1不进入
				times.add(maxj);
				maxj -= 2;
			}
			int plus = times.size() + 1;
			sum = sum + arr[i] * plus;
//			System.out.println(i+" -> "+plus);
		}
		return sum;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		System.out.println("-------");
		so.debug2();
		System.out.println("-------");
		so.debug3();
		System.out.println("-------");

//		int result = so.sumOddLengthSubarrays(new int[] { 1, 4, 2, 5, 3 });
//		System.out.println(result);
//		so.sumOddLengthSubarrays(new int[] { 1, 4, 2, 5, 3, 6, 8 });
	}

}
